﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Data.Common;
using System.Data.Entity;

namespace MemoLib
{
    public class MemoBookConfig
    {
        public const string CONFIGNAME_GlobalFlow = "GlobalFlow";
        //{0} is csdl name, {1} is database file full path
        public const string ConnectionStringFormat = "metadata=\"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl\";provider=System.Data.SQLite;provider connection string=\"data source={1}\"";
        //private const string SystemMetadataName = "EZMemo";
        private const string BookMetadataName = "MemoBook";
        private const string DBFileFolder = "Data";
        private const string DBFileName = "MemoBook";
        private const string DBFileExt = ".db3";


        public static string GetEFConnectionStringForBook( int bookID)
        {
            string metadataName = BookMetadataName;
            string dbFileName = string.Format("{0}{1}", bookID, DBFileExt);

            return GetEFConnectionString(metadataName, dbFileName);
        }

        public static string GetEFConnectionString()
        {
            string metadataName = BookMetadataName;
            string dbFileName = string.Format("{0}{1}", DBFileName, DBFileExt);

            return GetEFConnectionString(metadataName, dbFileName);
        }

        public static string GetEFConnectionString(string metadataName, string dbFileName)
        {
            if (string.IsNullOrEmpty(metadataName))
            {
                metadataName = BookMetadataName;
            }
            string dbFilePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),DBFileFolder, dbFileName);
            string connectionString = string.Format(ConnectionStringFormat, metadataName, dbFilePath);

            return connectionString;
        }


        public static DBContext GetDBContext( int bookID)
        {
            string str = GetEFConnectionStringForBook(bookID);

            //var connection = new EntityConnection(str);

            //using (EntityConnection conn = connection)
            //{
            //    conn.Open();

            //    Console.WriteLine("Just testing the connection.");
            //    conn.Close();
            //}

            DBContext dbcontext = new DBContext(str);

            return dbcontext;

        }

        //public static EntityConnection GetEFConnection()
        //{
        //    string str = GetEFConnectionString();

        //    var connection = new EntityConnection( str);

        //    using (EntityConnection conn = connection )
        //    {
        //        var mw = conn.GetMetadataWorkspace();
        //        conn.Open();

        //        Console.WriteLine("Just testing the connection.");
        //        conn.Close();
        //    }

        //    //try
        //    //{
        //    //    DBContext dbcontext = new DBContext(str);
        //    //    var dbconn = dbcontext.Database.Connection;
        //    //    //return (EntityConnection)dbconn;

        //    //    dbcontext.MemoConfig.Add(new MemoConfig { ConfigName = "test", ConfigValue = "100", ConfigValueType = "" });
        //    //    dbcontext.SaveChanges();
        //    //}
        //    //catch ( Exception ex)
        //    //{
        //    //}

        //    return connection ;
        //}


        public static int SaveConfig(string name, string value)
        {
            var database = new DBContext();

            
            var config = database.MemoConfig.Where(c => c.ConfigName == name).FirstOrDefault ();
            if (config == null)
            {
                config = new MemoConfig { ConfigName = name, ConfigValue = value, ConfigValueType = value.GetType().ToString() };
                database.MemoConfig.Add (config);
            }
            else
            {
                config.ConfigValue = value ;
            }

            int rc = database.SaveChanges();
            return rc;
        }

        public static string LoadConfig( string name )
        {
            var database = new DBContext();
            

            var config = database.MemoConfig.Where(c => c.ConfigName == name).FirstOrDefault ();

            string configvalue = null;
            if (config != null)
                configvalue = config.ConfigValue;

            return configvalue;
        }
    }
}
